const formatTime = date => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}
const YYYYMM = date => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1

  return [year, month].map(formatNumber).join('-')
}
const formatTimexg = date => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

	return [year, month, day].map(formatNumber).join('/');
}
const formatTime1 = date => {
  if (typeof (date) != 'object') {
    if (date.indexOf('T') > -1) { } else {
      date = date.replace(/-/g, '/')
    }
  }
  // 备选方案
  // date = String(date)
  // date = date.replace(/T/g, ' ')
  //  date = date.replace(/-/g, '/')
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours();
  const minute = date.getMinutes();
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatTime2 = date => {
  if (typeof (date) != 'object') {
    if (date.indexOf('T') > -1) { } else {
      date = date.replace(/-/g, '/')
    }
  }
  // 备选方案
  // date = String(date)
  // date = date.replace(/T/g, ' ')
  //  date = date.replace(/-/g, '/')
  date = new Date(date)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hour = String(date.getHours()).padStart(2, '0');
  const minute = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return year + '年' + month + '月' + day + '日' + ' ' + hour + ':' + minute + ':' + seconds
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDistance(lat1, lng1, lat2, lng2) {
  console.log("-----经纬度-----", lat1, lat2, lng1, lng2)
  var radLat1 = lat1 * Math.PI / 180.0;
  var radLat2 = lat2 * Math.PI / 180.0;
  var a = radLat1 - radLat2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;

}
function GetQueryString(url) {
  if (url.indexOf("?") != -1) {
    var index = url.indexOf('?') + 1;
    // 得到？后的字符串
    var str = url.substr(index); // postid=10457794&actiontip=保存修改成功')
    var paramsObj = {};
    // 字符串通过&标识，转为数组
    var arrs = str.split("&"); // ["postid=10457794", "actiontip=保存修改成功"]
    for (var i = 0; i < arrs.length; i++) {
      // 分别将 = 左右两边拆分为数组, 动态变为键值对
      paramsObj[arrs[i].split("=")[0]] = arrs[i].split("=")[1]
      //  arrs[i].split("=")   ["postid", "10457794"]
    }
  }
  return paramsObj
}

function getMonday(date) {
  let day = new Date(date).getDay() || 7;
  let mondayTime = new Date(date).getTime() - ((day - 1) * 24 * 60 * 60 * 1000);
  return formatTime(mondayTime);
}

const hour = date => {
  const date1 = new Date(date)
  const day = date1.getDate();
  const hour = date1.getHours()
  const minute = date1.getMinutes()
  const second = date1.getSeconds()
  console.log([hour + day * 24, minute, second].map(formatNumber).join(':'))
  return [hour + day * 24, minute, second].map(formatNumber).join(':')
}
const getTime = function (startTime, mark) {
  startTime = startTime.replace(/-/g, '/')
  var newDate = new Date().getTime();
  var reciprocal = (new Date(startTime).getTime() - newDate) / 1000;
  if (reciprocal < 0) {
    return '';
  }

  // 获取天、时、分、秒
  let day = parseInt(reciprocal / (60 * 60 * 24));
  if (day > 0) {
    return `${day}天`
  }
  let hour = parseInt(reciprocal % (60 * 60 * 24) / 3600);
  let minute = parseInt(reciprocal % (60 * 60 * 24) % 3600 / 60);
  let second = parseInt(reciprocal % (60 * 60 * 24) % 3600 % 60);
  if (mark == 'newjoblist') {
    return [hour + day * 24, minute].map(formatNumber).join(':')
  }
  return [hour + day * 24, minute, second].map(formatNumber).join(':')
}

const getAge = (UUserCard) => {
  var myDate = new Date();
  var month = myDate.getMonth() + 1;
  var day = myDate.getDate();
  var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
  if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
    age++;
  }
  return age
}
function getRpx(){ 
  var winWidth = wx.getSystemInfoSync().windowWidth;
   return 750/winWidth;
}

function isPtInPoly(aLat, aLon, pointList) {
	// 判断经纬度是否在多边形区域内
	/* 
	:param aLon: double 经度 
	:param aLat: double 纬度 
	:param pointList: list [{latitude: 22.22, longitude: 113.113}...] 多边形点的顺序需根据顺时针或逆时针，不能乱 
	*/
	var iSum = 0  
	var iCount = pointList.length
	  
	if(iCount < 3) {
		return false 
	}
	//  待判断的点(x, y) 为已知值
	var y = aLat
	var x = aLon
	for(var i = 0; i < iCount; i++) {
		var y1 = pointList[i].latitude  
		var x1 = pointList[i].longitude
		if(i == iCount - 1) {
			var y2 = pointList[0].latitude
			var x2 = pointList[0].longitude
		} else {
			var y2 = pointList[i + 1].latitude  
			var x2 = pointList[i + 1].longitude
		}
		// 当前边的 2 个端点分别为 已知值(x1, y1), (x2, y2)
		if (((y >= y1) && (y < y2)) || ((y >= y2) && (y < y1))) {
			//  y 界于 y1 和 y2 之间
			//  假设过待判断点(x, y)的水平直线和当前边的交点为(x_intersect, y_intersect)，有y_intersect = y
			// 则有（2个相似三角形，公用顶角，宽/宽 = 高/高）：|x1 - x2| / |x1 - x_intersect| = |y1 - y2| / |y1 - y|
			if (Math.abs(y1 - y2) > 0) {
				var x_intersect = x1 - ((x1 - x2) * (y1 - y)) / (y1 - y2);  
				if(x_intersect < x) {
					iSum += 1 
				}
			}
		} 
	}
	if(iSum % 2 != 0) {
		return true  
	}else {
		return false 
	}  
  }
function getHHMM(date) { 
	date = new Date(date)
	const hour = date.getHours();
	const minute = date.getMinutes();
	return [hour , minute].map(formatNumber).join(':')
}
function getPrevMonth(date) { 
	var date = new Date(date); 
    var year = date.getFullYear();
    var month =date.getMonth() ;
    if(month == 0){
         year = year -1;
         month = 12; 
    }
    return year+"-"+formatNumber(date.getMonth()+1);
}
function timeDifference(startTime, endTime) { //可以传日期时间或时间戳
	let start = typeof(startTime) == "number" ? startTime : new Date(startTime).getTime(),
		end = typeof(endTime) == "number" ? endTime : new Date(endTime).getTime(),
		difference = end - start, //时间差的毫秒数
		days = Math.floor(difference / (24 * 3600 * 1000)), //计算出相差天数
		leave1 = difference % (24 * 3600 * 1000), //计算天数后剩余的毫秒数
		hours = Math.floor(leave1 / (3600 * 1000)), //计算相差分钟数
		leave2 = leave1 % (3600 * 1000), //计算小时数后剩余的毫秒数
		minutes = Math.floor(leave2 / (60 * 1000)), //计算相差秒数
		leave3 = leave2 % (60 * 1000), //计算分钟数后剩余的毫秒数
		seconds = Math.round(leave3 / 1000);
	return `${hours}小时${minutes}分钟`
}
module.exports = {
	getHHMM,
	isPtInPoly: isPtInPoly,
	formatTime: formatTime,
	formatTimexg,
  formatTime1,
  formatTime2,
  formatNumber,
  getDistance: getDistance,
  GetQueryString: GetQueryString,
  getMonday,
  hour,
  getTime,
  getAge,
	getRpx,
	timeDifference,
	YYYYMM,
	getPrevMonth
}